<?php
if($_REQUEST['_a'] == "del") {
	
	$sql = "select stored_image_name from manage_images where id='".$_REQUEST['_d']."'";
	$res = mysql_query($sql) or die(mysql_error());
	$data = mysql_fetch_array($res);
	if(file_exists("../uploads/".$data['stored_image_name'])) {
		unlink("../uploads/".$data['stored_image_name']);
	}
	
	$sql = "delete from manage_images where id='".$_REQUEST['_d']."'";
	mysql_query($sql) or die(mysql_error());
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
  $('a[rel*=facebox]').facebox()
}) 
</script>
<script type="text/javascript">
function show()
{
	document.adfrm.action="index.php?tc=ads_add";
	document.adfrm.submit();
}
</script>
<form name="adfrm" method="post" action="#">
<table border="0" width="100%" align="center">
<tr>
	<td align="left"><span style="color:#1B78B7; font-size:14px; font-weight:bold">Images</span>&nbsp;
	</td>
    <td width="90%" align="right" class="font12">
    <a href="index.php?tc=add_images" class="font12">Add Images</a>  
    <!--<a href="index.php?tc=addcity" class="font12">Add City</a>-->
    </td>
	<td align="right">
    	<select name="_st" onChange="window.location='index.php?tc=images&page=<?php echo $_REQUEST['page'];?>&_st='+this.value">
        	<option value="" <?php if($_REQUEST['_st']=="") echo "selected";?>><--Sort--></option>
            <option value="2" <?php if($_REQUEST['_st']==2) echo "selected";?>>All</option>
            <option value="1" <?php if($_REQUEST['_st']==1) echo "selected";?>>Approved Images</option>
            <option value="3" <?php if($_REQUEST['_st']==3) echo "selected";?>>Pending Images</option>
        </select>	    
    </td>
</tr>
<tr>
	<td background="images/topline.gif" colspan="2" height="11"></td>
</tr>    
</table>
</form> 
	<?php
if($_REQUEST['_s'] == 2)
{ ?>
	<table align="center" border="0">
	<tr>
    	<td align="center"><div class="sucmsg"><img src="images/information.png" align="absmiddle" />&nbsp;Deleted Successfully</div></td>
    </tr>    
    </table>
<?php } ?>
<table width="100%" align="center" border="0" class="list">
<thead>
<tr>
	<td align="center" width="30">#</td>
	<td align="center" width="150">Image Name</td>
    <td align="center" width="300">Url</td>
    <td align="center" width="150">View Image</td>
    <td align="center" width="150">Status</td>
    <td align="center">Action</td>
</tr>
</thead>
<?php
include('includes/function.php');
$self="index.php?tc=images&_st=".$_REQUEST['_st'];
$rowsPerPage = 20;
$pageNum = 1;
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}
if($_GET['page']=="")
{
	$pageNum = 1;
}

/*<!--counting the offset-->*/
$offset = ($pageNum - 1) * $rowsPerPage;
$st = $_REQUEST['_st'];

if($st == "" || $st == 2)
{
	$res = mysql_query("select id from manage_images");
	
	$query = mysql_query("select `id`, `title`, `image`, `stored_image_name`,active_status, url  
						  from manage_images order by id desc limit $offset , $rowsPerPage") or die(mysql_error());
}
else
{
	if($st == 3) $st=0;
	$res = mysql_query("select id from manage_images where active_status='$st' ");
	
	$query = mysql_query("select `id`, `title`, `image`, `stored_image_name`,active_status, url  
						 from manage_images where active_status='$st' order by id desc 
						 limit $offset , $rowsPerPage");
}
$i = 1;
while($q = mysql_fetch_array($query))
{ 

	if($q["active_status"] == 1) $status = "Approved";
	else $status = "Pending";
	?>
    <tr>
        <td align="center"><?php echo $i+$offset;?></td>
        <td align="left"><?php echo $q["title"];?></td>
        <td align="left"><?php echo $q["url"];?></td>
        <td align="right"><a rel="facebox" href="../uploads/<?php echo $q['stored_image_name'];?>"/><img width="161px" src="../uploads/<?php echo $q['stored_image_name'];?>"/></a></td>
        <td align="center"><?php echo $status;?></td>
        <td align="center"><a href="index.php?tc=add_images&_d=<?php echo $q['id'];?>&page=<?php echo $pageNum;?>">Edit</a>&nbsp;
        	<a href="index.php?tc=images&_d=<?php echo $q['id'];?>&page=<?php echo $pageNum;?>&_a=del" onclick="return confirm('Are you sure want to delete?')">Delete</a>
        </td>
   	</tr>     
<?php 
	$i++;
}
?>
<?php 
if(mysql_num_rows($res) <= $rowsPerPage){	}
else { ?>
<tr>
    <td colspan="7" align="center"  bgcolor="#E3ECF3">
        <?php echo $paginglink=getPagingLink($res, $rowsPerPage,$pageNum,$self); ?>	
    </td>
</tr>
<?php } ?>
</table>    